package com.innovation.chengzhangriji.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.innovation.chengzhangriji.Baby;
import com.innovation.chengzhangriji.ChZhRJApplication;
import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.Parent;
import com.innovation.chengzhangriji.database.DatabaseManager;
import com.innovation.chengzhangriji.http.BabyAllResponse;
import com.innovation.chengzhangriji.http.Client;
import com.innovation.chengzhangriji.http.ResponseHandler;
import com.innovation.chengzhangriji.preferences.LoginInfo;
import com.innovation.chengzhangriji.ui.event.BaseEvent;

import de.greenrobot.event.EventBus;

public class GetAllParentInfoService extends Service {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        LoginInfo loginInfo = Config.getLoginInfo(this);
        if (loginInfo == null) {
            return super.onStartCommand(intent, flags, startId);
        }
        String phone = loginInfo.getPhone();
        String token = loginInfo.getToken().getAccessToken();
        String userId = loginInfo.getUserID();
        // 查询所有宝贝Id
        Baby[] babies = DatabaseManager.getInstance(this).queryBabyAll(userId);
        for (Baby baby : babies) {
            requestParentInfo(phone, userId, token, baby.getBabyID());
        }

        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void requestParentInfo(String phone, final String userId, String token, final int babyId) {
        Client.requestBabyAll(this, phone, token, babyId, new ResponseHandler() {

            @Override
            public void onInnovationSuccess(JsonElement value) {
                JsonObject object = value.getAsJsonObject();
                JsonArray jsonArray = object.getAsJsonArray("Messages");
                BabyAllResponse[] responses = get(jsonArray.toString(), BabyAllResponse[].class);
                if (responses == null) {
                    return;
                }
                Parent[] parents = new Parent[responses.length];
                for (int i = 0; i < responses.length; i++) {
                    parents[i] = new Parent();
                    parents[i].setBabyId(babyId);
                    parents[i].setType(responses[i].getType());
                    parents[i].setParentId(responses[i].getUserId());
                    parents[i].setUser(responses[i].getUser());
                    parents[i].setName(responses[i].getName());
                    parents[i].setPhone(responses[i].getPhone());
                    parents[i].setServerTS(responses[i].getServerTS());
                    parents[i].setMessage(responses[i].getMessage());
                    parents[i].setPath(responses[i].getPath());
                    parents[i].setNickName(responses[i].getNickName());
                    parents[i].setRole(responses[i].getAuth().getRole());
                    parents[i].setPermission(responses[i].getAuth().getPermission());
                    parents[i].setRange(responses[i].getAuth().getRange());
                }
                DatabaseManager.getInstance(GetAllParentInfoService.this).deleteAllParents(babyId);
                DatabaseManager.getInstance(GetAllParentInfoService.this).insertOrReplaceInTxParent(parents);
                // 发送通知
                BaseEvent event = new BaseEvent(BaseEvent.TYPE_LOAD_BABY_PARENT);
                EventBus.getDefault().post(event);
            }
        }, null);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        ChZhRJApplication.getRefWatcher(this).watch(this);
    }
}
